package org.lex.game.sudoku.view;

import org.lex.game.sudoku.SudokuApplication;
import org.lex.game.sudoku.util.Formatter;
import org.lex.game.sudoku.util.table.LabelProvider;

import common.Sudokus;

public class HistoryOperationLabelProvider implements LabelProvider<Operation> {
	private static final String[] COLUMN_NAMES = { "When", "Where", "Num" };

	private final SudokuApplication application;

	public HistoryOperationLabelProvider(SudokuApplication application) {
		this.application = application;
	}

	@Override
	public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return COLUMN_NAMES[columnIndex];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		Class<?> clz = Object.class;
		switch (columnIndex) {
		case 0:// when
			clz = String.class;
			break;
		case 1:// where
			clz = String.class;
			break;
		case 2:// new value
			clz = String.class;
			break;
		default:
			throw new RuntimeException("Column[" + columnIndex + "] undefined!");
		}
		return clz;
	}

	@Override
	public Object getColumnValue(Operation o, int columnIndex) {
		Object result = null;
		switch (columnIndex) {
		case 0:
			result = Formatter.DurationFormat.SHORT.format(o.getWhen() - this.application.getStartTime());
			break;
		case 1:
			result = (o.getWhere().x + 1) + ", " + (o.getWhere().y + 1);
			break;
		case 2:
			result = Sudokus.EMPTY_NUMBER == o.getNewValue() ? "" : Integer.toString(o.getNewValue());
			break;
		default:
			throw new RuntimeException("Column[" + columnIndex + "] undefined!");
		}
		return result;
	}

	@Override
	public int getPreferredColumnWidth(int columnIndex) {
		int width = 50;
		switch (columnIndex) {
		case 0:// when
			width = 80;
			break;
		case 1:// where
			width = 40;
			break;
		case 2:// new value
			width = 36;
			break;
		default:
			throw new RuntimeException("Column[" + columnIndex + "] undefined!");
		}
		return width;
	}
}
